﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MetroStyle
{
    public partial class MainPanel : Panel
    {
        public MainPanel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 主窗口
        /// </summary>
        public System.Windows.Forms.Form MainForm { get; set; }
        public Color BorderColor { get; set; }
        public int BorderSize { get; set; }
        public int BorderAlpha { get; set; }

        #region 任意位置拖动窗口
        private bool isdrag = false;
        private Point mousepoint;
        private void MainPanel_MouseDown(object sender, MouseEventArgs e)
        {
            isdrag = true;
            mousepoint = Cursor.Position;
        }

        private void MainPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (isdrag)
            {
                Point temp=Cursor.Position;
                MainForm.Location=new Point(MainForm.Location.X+(temp.X-mousepoint.X),MainForm.Location.Y+(temp.Y-mousepoint.Y));
                mousepoint = temp;
            }
        }

        private void MainPanel_MouseUp(object sender, MouseEventArgs e)
        {
            isdrag = false;
        }


        #endregion

        #region 绘制窗口边缘
        Graphics g;
        private void MainPanel_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            DrawBorder();
        }

        private void MainPanel_SizeChanged(object sender, EventArgs e)
        {
            g = this.CreateGraphics();
            g.Clear(this.BackColor);
            DrawBorder();
        }
        private void DrawBorder()
        {
            int sizeadd = BorderSize % 2 == 0&&BorderSize!=1 ? 0 : 1;
            Pen p = new System.Drawing.Pen(Color.FromArgb(BorderAlpha, BorderColor), BorderSize+sizeadd);
            g.DrawLine(p, new Point(0, BorderSize/2), new Point(this.Width, BorderSize/2));
            g.DrawLine(p, new Point(this.Width - (BorderSize/2), BorderSize), new Point(this.Width - (BorderSize/2), this.Height-BorderSize));

            g.DrawLine(p, new Point(BorderSize/2, BorderSize), new Point(BorderSize/2, this.Height-BorderSize));
            g.DrawLine(p, new Point(0, this.Height - (BorderSize/2)), new Point(this.Width, this.Height - (BorderSize/2)));
            g.Dispose();
            p.Dispose();
        }
        #endregion
    }
}
